package com.matt.components.aop;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.domain.*;
import com.matt.foundation.annotation.OptsTrace;
import com.matt.foundation.utils.ModelUtil;
import com.matt.foundation.vo.ClienteleSaveVo;
import com.matt.service.LeadsOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * **********************
 * description:线索操作日志
 * Created by Ron
 * from com.matt
 * on 2018/7/23 0023.
 * **********************
 */
@Component
@Aspect
@Slf4j
public class LeadsOperationLogAop {

    @Autowired
    private LeadsOperationLogService leadsOperationLogService;

    @Pointcut("execution(* com.matt.service.impl.*ServiceImpl.*(..)) ")
    public void executeService() {

    }

    @Around("executeService()  && @annotation(optsTrace)")
    public Object aroundTest(ProceedingJoinPoint joinPoint, OptsTrace optsTrace) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        Object[] args = joinPoint.getArgs();

        matchSave(request, optsTrace.code(), optsTrace.params(), args);
        return joinPoint.proceed();
    }


    /**
     * 不同操作类型进行匹配
     * @param request
     * @param code
     * @param params
     * @param args
     * @return
     */
    public LeadsHistory matchSave(HttpServletRequest request, String code, String[] params, Object[] args) {
        switch (code){
            /** 1.线索日志   ***/
            //线索修改
            case "10006":
            case "10001":
                return leadsOperationLogService.createLeads((Leads)args[0],ModelUtil.getSafeStr(args[1]), code);
            //线索删除
            case "10002":
                return leadsOperationLogService.deleteById(ModelUtil.getSafeStr(args[0]),ModelUtil.getSafeStr(args[1]),code);
            //线索转移
            case "10003":
                return leadsOperationLogService.changeStaffInfo(ModelUtil.getSafeStr(args[0]),ModelUtil.getSafeStr(args[1]),ModelUtil.getSafeStr(args[2]),ModelUtil.getSafeStr(args[3]));
            //线索转为客户
            case "10004":
                return leadsOperationLogService.changeClientele(ModelUtil.getSafeStr(args[0]),(ClienteleSaveVo)args[1],ModelUtil.getSafeStr(args[2]));
            //客户、线索、合同  上传附件
            case "10005":
                return leadsOperationLogService.uploadAttachment(ModelUtil.getSafeStr(args[0]),(Attachment)args[1]);
           /** 3.联系人   ***/
            //线索修改
            case "30005":
                return leadsOperationLogService.createLead((LeadsUser)args[0],ModelUtil.getSafeStr(args[1]), code);
            /** 1.客户日志   ***/
            //客户修改
            case "20001":
                return leadsOperationLogService.updateClientele(ModelUtil.getSafeStr(args[0]), (Clientele)args[1]);
            //客户转移
            case "20002":
                return leadsOperationLogService.updateClienteleStaff(ModelUtil.getSafeStr(args[0]),ModelUtil.getSafeStr(args[1]),ModelUtil.getSafeStr(args[2]),ModelUtil.getSafeStr(args[3]));
            //客户删除
            case "20003":
                return leadsOperationLogService.deleteClientele(ModelUtil.getSafeStr(args[0]),ModelUtil.getSafeStr(args[1]));
            //客户添加合同
            case "20004":
                return leadsOperationLogService.addContractForClientele((StaffDTO) args[0], (Contract)args[1]);
                default:
                return null;

        }
    }


}
